﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace _00365.Employees
{
    public partial class ManageEmployees : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadInitialData();
            }
        }

        private void LoadInitialData()
        {
            ddlSubsidiary.DataSource = Biz.Company.GetSubsidiaries();            
            ddlSubsidiary.DataBind();
            ddlSubsidiary.Items.Insert(0, new ListItem("Todos", (new Guid()).ToString()));
            grdEmployee.DataSource = Biz.Employee.GetAll(Biz.Sys.RowState.True);
            grdEmployee.DataBind();
        }

        protected void grdEmployee_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "EditEmployee":
                    Session["EmployeeFormAction"] = new Guid(e.CommandArgument.ToString());
                    Response.Redirect("EditEmployee.aspx?employee=" + e.CommandArgument.ToString());
                    break;
                case "DeleteEmployee":
                    hidSelected.Value = e.CommandArgument.ToString();
                    lblDeleteMsg.Text = string.Format(lblDeleteMsg.Text, Biz.Employee.Get(new Guid(e.CommandArgument.ToString())).FullName);
                    popDeleteMsg.Show();
                    break;
                default:
                    break;
            }
        }

        protected void btnDeleteMsgYes_Click(object sender, EventArgs e)
        {
            Biz.Employee.Remove(Biz.Employee.Get(new Guid(hidSelected.Value)));            
            hidSelected.Value = "";
            popDeleteMsg.Hide();
        }

        protected void btnDeleteMsgNo_Click(object sender, EventArgs e)
        {
            hidSelected.Value = "";
            popDeleteMsg.Hide();
        }

        protected void grdEmployee_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdEmployee.PageIndex = e.NewPageIndex;
        }

        protected void btnClosePopup_Click(object sender, EventArgs e)
        {
            popDeleteMsg.Hide();
        }

        protected void grdEmployee_PageIndexChanged(object sender, EventArgs e)
        {
            LoadInitialData();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            lblParameterValidation.Text = "";

            if (ValidadeForm())
            {
                grdEmployee.DataSource =
                    Biz.Employee.Search(
                          new Guid(ddlSubsidiary.SelectedValue)
                        , rdlGender.SelectedValue
                        , txtPosition.Text.Trim()
                        , txtDepartment.Text.Trim()
                        , rdlDependent.SelectedValue
                        , ddlMarriageStatus.SelectedValue
                        , Convert.ToInt32(txtAge.Text.Trim())
                        , ddlSchoolDegree.SelectedValue
                        , txtPeriod.Text.Trim()
                        , Convert.ToInt32(txtWorkingPeriod.Text.Trim())
                        , txtOccupation.Text.Trim()
                        , Convert.ToDecimal(txtRevenueFrom.Text.Replace(',', '.').Trim())
                        , Convert.ToDecimal(txtRevenueTo.Text.Replace(',', '.').Trim())
                        );
                grdEmployee.DataBind();
            }
            else
            {
                lblParameterValidation.Text = "Um ou mais parâmetros contém valores inválidos para a pesquisa.";
            }
        }

        private bool ValidadeForm()
        {
            int intReturn;
            decimal decimalReturn;

            if (!Int32.TryParse(txtAge.Text.Trim(), out intReturn))
                return false;

            if (!Int32.TryParse(txtWorkingPeriod.Text.Trim(), out intReturn))
                return false;

            if (!Decimal.TryParse(txtRevenueFrom.Text.Trim(), out decimalReturn))
                return false;

            if (!Decimal.TryParse(txtRevenueTo.Text.Trim(), out decimalReturn))
                return false;

            return true;
        }
    }
}